<template>
	<view>
		<u-popup
			v-model="show"
			mode="center"
			:mask="true"
			:popup="false"
			:closeable="false"
			:maskCloseAble="false"
			:safeAreaInsetBottom="true"
			:z-index="zIndex"
			:borderRadius="20"
			close-icon-color="#2B85E4"
			@close="success"
		>
			<view class="u-p-20">
				<view class="u-p-10">微信小程序登录</view>
				<view class="u-p-40 u-m-t-40 u-text-center" style="width:80vw;">
					<u-icon name="weixin-fill" color="#1aad19" size="220"></u-icon>
					<button type="primary" class="u-m-t-40" @click="getUserInfo">微信授权登录</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
/**
 * m-login-wxapp 小程序登录组件
 * @property {string} success 成功后返回的数据
 * @property {String} z-index 弹出时的z-index值
 */
export default {
	name: 'm-login-wxapp',
	props: {
		zIndex: {
			type: [String, Number],
			default: 0
		}
	},
	data() {
		return {
			code: '',
			user: {},
			show: false,
			resolve: null,
			reject: null
		};
	},
	methods: {
		// 本地用户数据
		local() {
			return new Promise((resolve, reject) => {
				this.reject = reject;
				this.resolve = resolve;
				this.$u.login.local().then(
					// 本地登录成功
					user => {
						this.user = user;
						this.success();
					},
					// 本地登录失败
					() => {
						this.user = {};
						this.checkLogin();
					}
				);
			});
		},
		// 重载用户数据
		reload() {
			return new Promise((resolve, reject) => {
				this.reject = reject;
				this.resolve = resolve;
				this.$u.login.reload().then(
					user => {
						this.user = that.$u.login.update(user);
						this.success();
					},
					() => {
						this.user = {};
						this.checkLogin();
					}
				);
			});
		},
		// 登录成功返回
		success() {
			this.show = false;
			this.resolve(this.user);
			this.$emit('success', this.user);
		},
		// 用户静默登录
		checkLogin() {
			let that = this;
			return new Promise((resolve, reject) => {
				uni.login({
					success(ret) {
						if (ret.code) {
							that.code = ret.code;
							that.$u.post('data/api.wxapp/session', { code: that.code }).then(res => {
								if (res.code > 0 && res.data.headimg) {
									that.user = that.$u.login.update(res.data);
									resolve(res.data);
									that.success();
								} else {
									reject(res.info);
									that.show = true;
								}
							});
						} else {
							that.$u.message.toast('调用微信授权失败！');
						}
					},
					fail() {
						that.$u.message.toast('调用微信授权失败！');
					}
				});
			});
		},
		// 小程序授权登录
		getUserInfo(e) {
			let that = this;
			uni.getUserProfile({
				lang: 'zh_CN',
				desc: '用于完善用户资料',
				fail(res) {
					if (res.errMsg === 'getUserProfile:fail auth deny') {
						return that.$u.message.toast('您取消了微信授权！');
					} else {
						that.$u.message.toast('调用微信授权失败！');
					}
				},
				success(res) {
					let data = {
						iv: res.iv,
						code: that.code,
						encrypted: res.encryptedData
					};
					that.$u.post('data/api.wxapp/decode', data).then(ret => {
						if (ret.code) {
							that.user = that.$u.login.update(ret.data);
							that.$u.message.toast('授权登录成功！');
							that.success();
						} else {
							that.reject(ret.info);
							that.$u.message.toast(ret.info);
						}
					});
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped></style>
